package br.mia.unifor.onaga.cloud.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class Execution {
	private Long id;
	private String name;
	private String description;
	private Boolean success;
	private Calendar when;
	private String workload;
	private String instanceType;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Boolean getSuccess() {
		return success;
	}

	public void setSuccess(Boolean success) {
		this.success = success;
	}

	public Calendar getWhen() {
		return when;
	}

	public void setWhen(Calendar when) {
		this.when = when;
	}

	public String getWorkload() {
		return workload;
	}

	public void setWorkload(String workload) {
		this.workload = workload;
	}

	public String getInstanceType() {
		return instanceType;
	}

	public void setInstanceType(String instanceType) {
		this.instanceType = instanceType;
	}

	@Override
	public String toString() {
		return "{\"name\":\"" + getName() + "\",\"id\":2,\"description\":\""
				+ getDescription() + "\",\"users\":" + getWorkload()
				+ ",\"when\":" + getWhen().getTimeInMillis() + ",\"success\":"
				+ getSuccess() + ",\"instanceType\":{\"name\":\""
				+ getInstanceType() + "\",\"id\":1}}";
	}

	public static void publishResult(Execution execution) {
		DefaultHttpClient httpclient = new DefaultHttpClient();
		try {
			HttpPost httpost = new HttpPost(
					"http://localhost:8080/OlioTestAnaliserWeb/servico/Execution");

			StringEntity stringEntity = new StringEntity(execution.toString(),
					"UTF-8");

			stringEntity.setContentType("application/json");

			httpost.setEntity(stringEntity);

			HttpResponse response = httpclient.execute(httpost);

			HttpEntity entity = response.getEntity();

			System.out.println("----------------------------------------");
			System.out.println(response.getStatusLine());
			if (entity != null) {
				System.out.println("Response content length: "
						+ entity.getContentLength());

				System.out.println(convertStreamToString(entity.getContent()));
			}
			EntityUtils.consume(entity);
		} catch (ClientProtocolException e) {
			Logger.getAnonymousLogger().log(Level.SEVERE, "", e);

		} catch (IOException e) {
			Logger.getAnonymousLogger().log(Level.SEVERE, "", e);

		} finally {
			httpclient.getConnectionManager().shutdown();
		}

	}

	public static String convertStreamToString(InputStream is)
			throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();
		String line = null;
		while ((line = reader.readLine()) != null) {
			sb.append(line + "\n");
		}
		is.close();
		return sb.toString();
	}
	
	public static void main(String[] args) {
		Execution execution = new Execution();
		
		execution.setName(args[0]);
		execution.setInstanceType(args[1]);
		
		execution.setDescription("MATHEUS - TESTE");
		execution.setSuccess(Boolean.FALSE);
		execution.setWhen( Calendar.getInstance());
		execution.setWorkload("45");
		
		Execution.publishResult(execution);
	}

}
